package com.recognition;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Logger {

	private static Logger logger;
	
	private static JFrame frame;
	private JPanel contentPane;
	
	private DefaultListModel<String> infoEntries;
	private DefaultListModel<String> errorEntries;
	private DefaultListModel<String> allEntries;
	private JList<String> all;
	private JList<String> info;
	private JList<String> error;
	
	private JScrollPane scrollPane;
	private JButton btnAll;
	private JButton btnError;
	private JButton btnInfo;
	private JButton btnReset;

	/**
	 * Launch the application.
	 */
	public static void initialize() 
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					logger.createWindow();	
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public static Logger getLogger()
	{				
		if (logger == null)
		{
			logger = new Logger();
			initialize();
		}
		
		return logger;
	}
	
	
	public static boolean showLog()
	{
		if (frame.isVisible())
		{
			frame.setVisible(false);
			return false;
		}
		
		frame.setVisible(true);
		
		JScrollBar sb = logger.scrollPane.getVerticalScrollBar();
		if (sb != null)
			sb.setValue(sb.getMaximum());
			
		return true;
	}
	
	/**
	 * Create the frame.
	 * @wbp.parser.entryPoint
	 */
	public void createWindow() {
		frame = new JFrame();
		frame.setTitle("Recognition Client: Log");		
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setBounds(100, 100, 402, 209);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		frame.setContentPane(contentPane);
			
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setPreferredSize(new Dimension(2000, 30));
		contentPane.add(panel, BorderLayout.NORTH);			
		
		btnAll = new JButton("All");
		btnAll.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				scrollPane.setViewportView(all);
			}
		});
		btnAll.setBounds(0, 0, 60, 25);
		panel.add(btnAll);
		
		btnError = new JButton("Error");
		btnError.setBounds(70, 0, 60, 25);
		btnError.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				scrollPane.setViewportView(error);
			}
		});
		panel.add(btnError);
		
		btnInfo = new JButton("Info");
		btnInfo.setBounds(140, 0, 60, 25);
		btnInfo.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				scrollPane.setViewportView(info);
			}
		});
		panel.add(btnInfo);
		
		
		btnReset = new JButton("Reset");
		btnReset.setBounds(287, 1, 89, 23);
		btnReset.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				allEntries.clear();
				infoEntries.clear();
				errorEntries.clear();
			}
		});
		panel.add(btnReset);
		
		scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		allEntries = new DefaultListModel<String>();
		infoEntries = new DefaultListModel<String>();
		errorEntries = new DefaultListModel<String>();		
		all = new JList<String>(allEntries);
		info = new JList<String>(infoEntries);
		error = new JList<String>(errorEntries);
		scrollPane.setViewportView(all);
	}
	
	public void log(String line)
	{		
		allEntries.add(allEntries.size(), line);
		
		JScrollBar sb = scrollPane.getVerticalScrollBar();
		if (sb != null)
			sb.setValue(sb.getMaximum());
	}
	
	public void info(String line)
	{
		infoEntries.add(infoEntries.size(), line);
		log(line);
	}
	
	public void error(String line)
	{
		errorEntries.add(errorEntries.size(), line);
		log(line);		
	}
	
	public void updatePos(int x, int y)
	{
		frame.setBounds(x, y, frame.getWidth(), frame.getHeight());
	}
}
